% read,display & scroll MT ts data in RTS format. 
% Provision for selecting stacks 
% Selection returned through ProcDef.SelStacks
% start 15.10.2001
% latest date 22.5.2003
% -----------declarations


function[er,out] = disprts(TS,ProcDef);
er = 1;


if nargin ==1,
   ProcDef = SetDefault(TS.head.band);
   ProcDef.file1=[num2str(setstr(TS.head.SiteID))' 'Band' num2str(TS.head.band)];
	ProcDef.len = floor(length(TS.matrix)/ProcDef.block);
   ProcDef.SelStacks = ones([1 ProcDef.len]);  
elseif nargin == 0,
	[fid,er,file] = getbfile('*.rts');
	if fid == -1 | er < 1,
      return;
	end;
	disp('Rts-in -> reading time series');
	[TS,fid] = rts_in(fid); % reading time series file (rts format - metronix)
   fclose(fid);
   ProcDef = SetDefault(TS.head.band);
   ProcDef.file1=file;
 %  ProcDef.block = 1024; %!!!!!! REMOVE THIS LINE 24.7.05 TOULOUSE
   ProcDef.len = floor(length(TS.matrix)/ProcDef.block);
   ProcDef.SelStacks = ones([1 ProcDef.len]);
      
end;

ProcDef.len = floor(length(TS.matrix)/ProcDef.block);

Labe = ['Ex';'Ey';'Hx';'Hy';'Hz'];
Labe1 = ['mV/km';'mV/km';'nT/ s';'nT/ s';'nT/ s'];
plchan=5;
block = ProcDef.block;

for i = 1:TS.head.nchannel,
factor(i) = NrFct(TS.head.band,ProcDef.sensor(i),TS.probe.dist(i),TS.probe.gain(i));
end;
factor(1:2) = factor(1:2)*0.040; %these factors were found necessary while 
factor(3:5) = factor(3:5)*5; % comparing with ProcMT 22.5.3

Nsq = block*block;

len = ProcDef.len;
file = ProcDef.file1;
start = 0;
st = start*block + 1;
en = (start+1)*block;
nchannel = TS.head.nchannel;
i = start + 1;
%---------- actual stuff ------------------------------

H1 = figure(1);
set(GCF,'numbertitle','off');
set(GCF,'name','raw time series /trend /normalization');


while(st),

for u = 1:TS.head.nchannel,
K = CorTre(TS.matrix(st:en,u),0)*factor(u);
Channel(u,:) = K';
end; 	
t1 = 1/TS.head.fs;
XX = (0:ProcDef.block-1).*t1;
%---------Plotting the 5 channels ----------------------
if ProcDef.ref==1,

	for wd = 1:plchan,
   subplot(plchan,1,wd);
   
   if wd >2 & wd  <5,
      plot(Channel([wd wd+3],1:block)');
   else,
      plot(Channel(wd,1:block));
   end;
   	axis([1 block -inf inf]);
	end;
else,
	for wd = 1:plchan,
   subplot(plchan,1,wd);
   plot(XX,Channel(wd,1:block),'k-');
   axis([0 inf -inf inf]);
   ylabel(Labe1(wd,:));
   text(XX(block)+2*XX(2),0,Labe(wd,:));
   end;
end;


xlabel(	sprintf('time (s) \n Stack %d of %d                                           file   : %s',i,len,file));

Index=ProcDef.SelStacks(i);

if Index == 1,
   Sell ='Y';
else,
   Sell = 'N';
end;


men1 = menu('NnDisc','->','-','Exit',Sell,'<-');

if men1 == 4,
   if Index == 1
      ProcDef.SelStacks(i)= 0;
   else,
      ProcDef.SelStacks(i)= 1;
   end;
end;

      
if men1 == 3,
   
   if nargout == 2,
      out = ProcDef;
   end;
   
close all;
return;
end;

%--------Aid FFT-----------

clear Channel;
clear KX;


if (men1 == 1 | men1==4) & en+block <= len*block,
st = en + 1;
en = (i+1)*block;
i = i+1;

elseif men1 == 5 & st-block >= 0,
st = st - block;
en = en - block;
i = i -1;
end;

end;
